<?php


class Application{

	/**
	 * ��ʼ��Ŀ¼��
	 */
	private static function _initDirConst() {
		define ( 'ROOT', str_replace ( "\\", "/", dirname ( dirname ( __FILE__ ) ) ) ); // �����·��
		define ( 'CONFIG_DIR', ROOT . '/Config' ); // �����ļ�·��
		define ( 'CONTROL_DIR', ROOT . '/Control' ); // ������·��
		define ( 'CORE_DIR', ROOT . '/Core' ); // ����·��
		define ( 'MODEL_DIR', ROOT . '/Model' ); // ģ��·��
		define ( 'PUBLIC_DIR', ROOT . '/Public' ); // ��Դ·��
		define ( 'VIEW_DIR', ROOT . '/View' ); // ��ͼ��ģ�壩·��
		define ( 'COMMON_DIR', ROOT . '/Common' );
		define ( 'TOOlS_DIR', CORE_DIR . '/Tools');
		define( 'SMARTYs_DIR', ROOT . '/Smarty' );
	}

	/**
	 * ��ʼ���ļ���
	 */
	private static function _initInclude() {
		include (CONFIG_DIR . '/Config.php');
		include (COMMON_DIR . '/Common.php');
		include (CORE_DIR . '/MysqlDB.class.php');
		include (CORE_DIR . '/Model.class.php');
		include(SMARTYs_DIR.'/Smarty.class.php');
		include (CORE_DIR . '/View.class.php');
		include (CORE_DIR . '/Control.class.php');
		include (CONTROL_DIR . '/CommonControl.class.php');
		
		$GLOBALS ['config'] = $config;
	}

	/**
	 * ��ʼ��URL����
	 */
	private static function _initParams() {
		if ($GLOBALS ['config'] ['URL_MODEL'] == 1) {
			$group = isset ( $_GET ['group'] ) ? $_GET ['group'] : $GLOBALS ['config'] ['DEFAULT_GROUP'];
			$module = isset ( $_GET ['module'] ) ? $_GET ['module'] : $GLOBALS ['config'] ['DEFAULT_MODULE'];
			$action = isset ( $_GET ['action'] ) ? $_GET ['action'] : $GLOBALS ['config'] ['DEFAULT_ACTION'];
		} else {
			$path_info=isset($_SERVER['PATH_INFO'])?$_SERVER['PATH_INFO']:'';
			$path_info=trim($path_info,'/');
			$info=explode('/',$path_info);//url��Ϣ
			$group=!empty($info[0])?$info[0]:$GLOBALS ['config'] ['DEFAULT_GROUP'];
			$module=isset($info[1])?$info[1]:$GLOBALS ['config'] ['DEFAULT_MODULE']; 	//��ȡģ����
			$action=isset($info[2])?$info[2]:$GLOBALS ['config'] ['DEFAULT_ACTION']; 	//��ȡ������
			$count=count($info);						//��Ϣ����
			if($count>3){							//��ȡ$_GET����
				for($i=3;$i<$count;$i=$i+2){
					$getname=$info[$i];
					$getvalue=$info[$i+1];
					$_GET[$getname]=$getvalue;
				}
			}
		}
		$group = ucfirst ( strtolower ( $group ) );
		$module = ucfirst ( strtolower ( $module ) );
		define ( 'GROUP', $group );
		define ( 'MODULE', $module );
		define ( 'ACTION', $action );
	}

	/**
	 * ��ʼ������Զ����ػ���
	 */
	private static function _initAutoload() {
		spl_autoload_register ( array (
		__class__,
		'autoload'
		) );
	}
	private static function autoload($classname) {
		if (file_exists(MODEL_DIR . '/' . $classname . '.class.php')) {
			include (MODEL_DIR . '/' . $classname . '.class.php');
		}else{
			include(TOOlS_DIR. '/' . $classname . '.class.php');
		}
	}

	/**
	 * ����ת��
	 */
	private static function _initDispatch() {
		session_start();
		if(GROUP!=='Article'){
			if(!get_magic_quotes_gpc()){
				$_GET=new_addslashes($_GET);
				$_POST=new_addslashes($_POST);
				$_COOKIE=new_addslashes($_COOKIE);
			}
		}
		$control = MODULE . 'Control';
		$action = ACTION;
		include ('Control/' . GROUP . '/' . $control . '.class.php');
		$obj = new $control ();
		$obj->$action ();
	}

	/**
	 * ��ʼ����
	 */
	public static function run() {
		self::_initDirConst (); // ��ʼ��Ŀ¼��
		self::_initInclude (); // ��ʼ���ļ���
		self::_initParams (); // ��ʼ���������
		self::_initAutoload (); // ��ʼ���Զ�����
		self::_initDispatch (); // ����ת��
	}
}